package Thread;

import java.util.Random;

/**
 * @ClassName RunThread
 * @Description TODO
 * @Author WSY
 * @Date 2020-6-10 15:27
 */

class Tortiose extends Thread {
    private int trcak = 20;
    Random rm = new Random();
    static {
        System.out.println("乌龟开始跑了");
    }
    @Override
    public void run() {
        while (trcak != 0) {
            switch (rm.nextInt(5)){
                case 2:
                    try{
                        System.out.println("乌龟睡了1秒");
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 0:
                    try{
                        System.out.println("乌龟睡了2秒");
                        Thread.sleep(2000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                default:
                    try{
                        trcak-=2;
                        System.out.println("乌龟前进了2米，距离终点"+trcak);
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
            }
        }
        if(trcak==0){
            System.out.println("乌龟胜利");
            System.exit(0);
        }
    }

}

class Rabbit extends Thread {
    private int trcak = 10;
    Random rm = new Random();
    static {
        System.out.println("兔子开始跑了");
        System.out.println("兔子先跑了十米");
    }
    @Override
    public void run() {
        while (trcak != 0) {
            switch (rm.nextInt(9)){
                case 0:
                    trcak-=10;
                    System.out.println("兔子前进了10米，距离终点"+trcak);
                    break;
                default:
                    try{
                        System.out.println("兔子睡了10秒");
                        Thread.sleep(10000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
            }
        }
        if(trcak==0){
            System.out.println("兔子胜利");
            System.exit(0);
        }
    }

}